import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:provider/provider.dart';
import '../utils/general.dart';
import '../components/fflistview.dart';
import '../utils/network/menu_model.dart';
import '../utils/general.dart';
class Coffee extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return CoffeeState();
  }
}

class CoffeeState extends State<Coffee> {

  bool bl = true;
  int leftSelectIndex = 0;
  ScrollController sc;

  @override
  void initState() {
    sc = ScrollController();
    sc.addListener((){
      if (sc.offset < 490 && leftSelectIndex != 0 && bl) {
        setState(() {
          leftSelectIndex = 0;
        });
      }  
      if (sc.offset > 490 && sc.offset <= 490 * 2 && leftSelectIndex != 1 && bl) {
        setState(() {
          leftSelectIndex = 1;
        });
      }
      if (sc.offset > 490 * 2 && leftSelectIndex != 2 && bl) {
        setState(() {
          leftSelectIndex = 2;
        });
      }

    });
    super.initState();
  }
  
  /// 状态栏
  Widget menuAppBar(BuildContext context) {
    double hei = statusBarHeight(context) + 44;
    return Container(
      height:hei,
      color:Colors.white,
      child:Padding(
        padding: EdgeInsets.only(top:hei-44),
        child: Center(
          child:Text(
            '选择咖啡喝小食',
            style:TextStyle(fontSize: 18, fontWeight:FontWeight.bold, color:Color.fromRGBO(56, 56, 56, 1.0)),
          )
        ),
      )
    );
  }
  /// banner
  Widget menuBanner(BuildContext context) {

    List list = [
      'assets/images/menu/swiper1.jpg',
      'assets/images/menu/swiper2.png'
    ];
    
    double scale = widthScale(context);
    
    return SizedBox(
      height: 130 * scale,
      child: Swiper(
        autoplay: true,
        itemCount: list.length,
        pagination: SwiperPagination(alignment:Alignment.bottomRight),
        onTap: (index){
          print(index);
        },
        itemBuilder: (context, index) {
          return  Image.asset(list[index], fit:BoxFit.fill);
        },
      ),
    );
  }
  /// 
  Widget menuLeftList(BuildContext context) {
    double scale = widthScale(context);
    // Map<String, dynamic> map = menuList as Map<String, dynamic>;
    List list = ['人妻top', '大师咖啡', '瑞纳冰'];
    return ListView.builder(
      itemCount: list.length,
      itemExtent: 44 * scale,
      padding: EdgeInsets.all(0),
      itemBuilder: (BuildContext context, int index){
        Widget temp = Container(
          color: index == leftSelectIndex ? Colors.white : Color.fromRGBO(248, 248, 248, 1.0),
          child:Row(
            children: <Widget>[
              Container(width: 3 * scale, color:index == leftSelectIndex ? Colors.blue : Colors.white),
              Expanded(
                child: GestureDetector(
                  onTap:() async {
                    if (leftSelectIndex != index) {
                      print(index);
                      setState(()=>leftSelectIndex = index);
                      double temp = 490 * index * 1.0;
                      bl = false;
                      await sc.animateTo(temp, duration: Duration(seconds:1), curve: Curves.easeIn);
                      bl = true;
                    }
                  
                  },
                  child:Stack(
                    children:[
                      Center(
                        child: Text(list[index], style:TextStyle(fontSize: 14, fontWeight: index == leftSelectIndex ? FontWeight.bold : FontWeight.normal, color: index == leftSelectIndex ? Colors.black87 : Colors.black54)),
                      ),
                    ]
                  )
                )
              ) 
            ],
          )
        );
        return temp;
      });
  }
  ///
  Widget menuRightList(BuildContext context) {
    ListView temp = ListView.builder(
      controller: sc,
      physics: ClampingScrollPhysics(),
      padding: EdgeInsets.only(top:0),
      itemBuilder: (context, index){
        return FFListView(title:'人气咖啡', subtitle:'');
      },
      itemCount: 3,
    );  

    
    return temp;
  }
   ///
  Widget menuList(BuildContext context) {

    double scale = widthScale(context);

    double need = screenHeight(context) - statusBarHeight(context) - 44 - 130 * scale - bottomTabBarHeight(context);

    return Stack(
      children:[
        Container(
          height: need,
        ),
        Positioned(
          left: 0,
          top: 0,
          child: Container(
            width: 90 * scale,
            height: need,
            color:Colors.white,
            child: menuLeftList(context),
          )
        ),
        Positioned(
          left: 90 * scale,
          top:0,
          child: Container(
            width: screenWidth(context) - 90 * scale,
            height: need,
            color:Colors.amber,
            child:menuRightList(context),
          )
        ),
      ]
    );
  }
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child:Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          menuAppBar(context),
          menuBanner(context),
          menuList(context)
        ],
      )
    );
  }
}